package com.xinzhidi.web.netty.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinzhidi.common.core.domain.model.LoginUser;
import com.xinzhidi.common.core.page.TableDataInfo;
import com.xinzhidi.common.utils.DateUtils;
import com.xinzhidi.common.utils.SecurityUtils;
import com.xinzhidi.common.utils.bean.BeanUtils;
import com.xinzhidi.web.netty.entity.AgreementUnitValveHistory;
import com.xinzhidi.web.netty.entity.dto.AgreementUnitValveHistoryAddDto;
import com.xinzhidi.web.netty.entity.dto.AgreementUnitValveHistoryListDto;
import com.xinzhidi.web.netty.entity.dto.AgreementUnitValveHistoryUpdDto;
import com.xinzhidi.web.netty.entity.vo.AgreementUnitValveHistoryInfoVo;
import com.xinzhidi.web.netty.entity.vo.AgreementUnitValveHistoryListVo;
import com.xinzhidi.web.netty.mapper.AgreementUnitValveHistoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;


/**
 * 单元阀门协议-历史数据Service业务层处理
 *
 * @author cx
 * @date 2023-06-17
 */
@Service
public class AgreementUnitValveHistoryService extends ServiceImpl<AgreementUnitValveHistoryMapper, AgreementUnitValveHistory> {
    @Autowired
    private AgreementUnitValveHistoryMapper agreementUnitValveHistoryMapper;

    /**
     * 查询单元阀门协议-历史数据
     *
     * @param id 单元阀门协议-历史数据主键
     * @return 单元阀门协议-历史数据
     */
    public AgreementUnitValveHistoryInfoVo selectAgreementUnitValveHistoryById(Long id) {
        AgreementUnitValveHistory agreementUnitValveHistory = agreementUnitValveHistoryMapper.selectById(id);
        AgreementUnitValveHistoryInfoVo infoVo = new AgreementUnitValveHistoryInfoVo();
        BeanUtils.copyProperties(agreementUnitValveHistory, infoVo);
        return infoVo;
    }

    /**
     * 查询单元阀门协议-历史数据列表
     *
     * @param dto 查询单元阀门协议-历史数据dto
     * @return 单元阀门协议-历史数据
     */
    public TableDataInfo<AgreementUnitValveHistoryListVo> selectAgreementUnitValveHistoryList(AgreementUnitValveHistoryListDto dto) {
        LambdaQueryWrapper<AgreementUnitValveHistory> wrapper = Wrappers.lambdaQuery();
        wrapper.orderByDesc(AgreementUnitValveHistory::getId);
        return TableDataInfo.getDataTable(agreementUnitValveHistoryMapper.selectList(wrapper), (domain -> {
            AgreementUnitValveHistoryListVo vo = new AgreementUnitValveHistoryListVo();
            BeanUtils.copyProperties(domain, vo);
            return vo;
        }));
    }

    /**
     * 新增单元阀门协议-历史数据
     *
     * @param dto 新增单元阀门协议-历史数据dto
     * @return 结果
     */
    public int insertAgreementUnitValveHistory(AgreementUnitValveHistoryAddDto dto) {
        LoginUser loginUser = SecurityUtils.getLoginUser();

        AgreementUnitValveHistory agreementUnitValveHistory = new AgreementUnitValveHistory();
        BeanUtils.copyProperties(dto, agreementUnitValveHistory);
        agreementUnitValveHistory.setCreateBy(loginUser.getUserId().toString());
        agreementUnitValveHistory.setCreateTime(DateUtils.getNowDate());
        return agreementUnitValveHistoryMapper.insert(agreementUnitValveHistory);
    }

    /**
     * 修改单元阀门协议-历史数据
     *
     * @param dto 修改单元阀门协议-历史数据dto
     * @return 结果
     */
    public int updateAgreementUnitValveHistory(AgreementUnitValveHistoryUpdDto dto) {
        LoginUser loginUser = SecurityUtils.getLoginUser();

        AgreementUnitValveHistory agreementUnitValveHistory = new AgreementUnitValveHistory();
        BeanUtils.copyProperties(dto, agreementUnitValveHistory);
        agreementUnitValveHistory.setUpdateBy(loginUser.getUserId().toString());
        agreementUnitValveHistory.setUpdateTime(DateUtils.getNowDate());
        return agreementUnitValveHistoryMapper.updateById(agreementUnitValveHistory);
    }

    /**
     * 批量删除单元阀门协议-历史数据
     *
     * @param ids 需要删除的单元阀门协议-历史数据主键
     * @return 结果
     */
    public int deleteAgreementUnitValveHistoryByIds(Long[] ids) {
        return agreementUnitValveHistoryMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除单元阀门协议-历史数据信息
     *
     * @param id 单元阀门协议-历史数据主键
     * @return 结果
     */
    public int deleteAgreementUnitValveHistoryById(Long id) {
        return agreementUnitValveHistoryMapper.deleteById(id);
    }
}
